package com.android.qjpmc.data_module;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.android.qjpmc.application_module.home.fragment.AlarmRecordslFragment;
import com.android.qjpmc.application_module.home.fragment.HistoryPageFragment;
import com.android.qjpmc.application_module.home.fragment.StationListFragment;
import com.android.qjpmc.application_module.home.fragment.ViewEventFragment;
import com.android.qjpmc.bean.AlarmRecordItemBean;
import com.android.qjpmc.bean.DeviceValueBean;
import com.android.qjpmc.bean.EventRecordItemBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.ReportDataItemBean;
import com.android.qjpmc.bean.StationDeviceBean;
import com.android.qjpmc.bean.SiteBean;
import com.android.qjpmc.bean.SiteStationBean;
import com.android.qjpmc.constant.Constant;
import com.android.qjpmc.kernel.JxtXmlPara;
import com.android.qjpmc.kernel.PwrDiagramObject;
import com.android.qjpmc.kernel.StateCompt;
import com.android.qjpmc.widget.retrofithelper.rxschedulers.RxSchedulersHelper;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.android.qjpmc.widget.utils.UIUtils;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class CUIObejcts
{
    StationListFragment m_StationListFragment = null;
    AlarmRecordslFragment m_AlarmRecordFragment = null;
    HistoryPageFragment m_HistoryPageFragment = null;
    ViewEventFragment m_EventFragment = null;


    //--------------------------------------------------------------
    public void SetStationListFragement(StationListFragment fragment)
    {
        m_StationListFragment = fragment;
    };
    public StationListFragment GetStationListFragment()
    {
        return m_StationListFragment;
    }

    //---------------------------------------------------------------
    public void SetAlarmRecordFragment(AlarmRecordslFragment fragment)
    {
        m_AlarmRecordFragment = fragment;
    };

    public AlarmRecordslFragment GetAlarmRecordFragment()
    {
        return m_AlarmRecordFragment;
    }

    //--------------------------------------------------------------
    public void SetHistoryEventsFragment(HistoryPageFragment fragment)
    {
        m_HistoryPageFragment = fragment;
    }

    HistoryPageFragment GetHistoryFragment()
    {
        return m_HistoryPageFragment;
    }

    //-----------------
    public void SetViewEventFragment(ViewEventFragment fragment)
    {
        m_EventFragment = fragment;
    }

    public ViewEventFragment GetEventFragment()
    {
        return m_EventFragment;
    }




};

public class PwrDataModule {

    public RtDataEntranceMnager m_RtDataEntranceMng = new RtDataEntranceMnager();

    //UI 对象----------------------------------------------
    CUIObejcts m_UIObjects = new CUIObejcts();

    public int SetStationListFragement(StationListFragment fragement)
    {
        if (m_UIObjects == null)
        {
            return Constant.GEN_ERROR;
        }
        m_UIObjects.SetStationListFragement(fragement);
        return Constant.NO_ERROR;
    };

    public StationListFragment GetStationListFragment()
    {
        if (m_UIObjects == null)
        {
            return null;
        }
        return m_UIObjects.GetStationListFragment();
    }

    public int SetHistoryFragement(HistoryPageFragment fragement)
    {
        if (m_UIObjects == null)
        {
            return Constant.GEN_ERROR;
        }
        m_UIObjects.SetHistoryEventsFragment(fragement);
        return Constant.NO_ERROR;
    };

    public HistoryPageFragment GetHistoryFragment()
    {
        if (m_UIObjects == null)
        {
            return null;
        }
        return m_UIObjects.GetHistoryFragment();
    }
    //--------------------------------------------
    public int SetAlarmFragement(AlarmRecordslFragment fragement)
    {
        if (m_UIObjects == null)
        {
            return Constant.GEN_ERROR;
        }
        m_UIObjects.SetAlarmRecordFragment(fragement);
        return Constant.NO_ERROR;
    };

    public AlarmRecordslFragment GetAlarmRecordsFragment()
    {
        if (m_UIObjects == null)
        {
            return null;
        }
        return m_UIObjects.GetAlarmRecordFragment();
    }
    //---------------------------------------------------
    public int SetEventFragment(ViewEventFragment fragment)
    {
        if (m_UIObjects == null)
        {
            return Constant.GEN_ERROR;
        }
        m_UIObjects.SetViewEventFragment(fragment);
        return Constant.NO_ERROR;
    }


    public ViewEventFragment GetEventFragment()
    {
        if (m_UIObjects == null)
        {
            return null;
        }
        return m_UIObjects.GetEventFragment();
    }



    //用户模块
    CUserModule m_user_module = new CUserModule();
    int m_StationDownLoad = 0;
    public int IsStationDownLoad()
    {
        return m_StationDownLoad;
    }
    public void SetStationDownMark(int mark)
    {
        m_StationDownLoad = mark;
    }

    int m_DataPointLoaded = 0;

    public int IsDataPointLoaded()
    {
        return m_DataPointLoaded;
    }

    public void SetDataPontLoadedMark(int Mark)
    {
        m_DataPointLoaded = Mark;
    }

    public CUserModule GetUserModule()
    {
        return m_user_module;
    }

    PwrStation m_CurrentDataStation = null;
    PwrDevice m_CurrentDataDevice = null;
    private void SetCurrentDataDevice(PwrDevice Device)
    {
        m_CurrentDataDevice = Device;
    }

    void SetCurrentDataStation(PwrStation station)
    {
        m_CurrentDataStation = station;
    }

    PwrStation GetCurrentDataStation()
    {
        return m_CurrentDataStation;
    }

    public PwrDevice ToFirstDataDevice()
    {
        PwrStation station = GetFirstStation();
        if (station == null)
        {
            SetCurrentDataStation(null);
            SetCurrentDataDevice(null);
            return null;
        }
        SetCurrentDataStation(station);
        PwrDevice Device = station.GetFirstDevice();
        SetCurrentDataDevice(Device);
        return Device;
    }

    public PwrStation ToNextDataStation()
    {
        PwrStation Station = GetCurrentDataStation();
        if (Station == null)
        {
            return null;
        }
        Station = Station.GetNext();
        SetCurrentDataStation(Station);
        return Station;
    }

    public void SetCurrentPwrStation(PwrStation Station)
    {
        m_CurrentStation = Station;
    }

    public PwrDevice GetCurrentDataDevice()
    {
        return m_CurrentDataDevice;
    }

    public PwrDevice ToNextDataDevice()
    {
        PwrDevice Device = GetCurrentDataDevice();
        if (Device == null)
        {
            return null;
        }
        Device = Device.GetNext();
        //如果是最后一个设备
        if (Device == null)
        {
            PwrStation station = ToNextDataStation();
            if (station == null)
            {
                SetCurrentDataDevice(null);
                return null;
            }
            Device = station.GetFirstDevice();
        }
        SetCurrentDataDevice(Device);
        return Device;
    }

    //报表数据查询
    public class ReportDataQuery
    {
        public String m_start_time;
        public String m_end_time;
        public int station_id;//站id
        public String station_name;
        public String device_name;
        public int device_id;
        public int new_query;
        public ArrayList<Integer> analog_list = new ArrayList<Integer>();
        public ArrayList<ReportDataItemBean> report_data_array = new ArrayList<ReportDataItemBean>();

        ReportDataQuery()
        {
            ReportDataItemBean bean = null;
            for (int idx = 0; idx < 100; idx++)
            {
                bean = new ReportDataItemBean();
                bean.value = Integer.toString(idx);
                bean.time = Integer.toString(idx%24) + "时";
                bean.unit = "V";
                bean.name = "Ua";
                report_data_array.add(bean);
            };
        }
    };

    //报警数据查询
    public class AlarmDataQuery
    {
        public String m_start_time;
        public String m_end_time;
        public int m_station_id;//站id
        public int m_device_id;
        public String station_name;
        public String device_name;
        public int new_query;
        public ArrayList<Integer> analog_list = new ArrayList<Integer>();
        public ArrayList<AlarmRecordItemBean> alarm_data_array = new ArrayList<AlarmRecordItemBean>();

        AlarmDataQuery()
        {
            AlarmRecordItemBean bean = null;
            for (int idx = 0; idx < 100; idx++)
            {
                bean = new AlarmRecordItemBean();
                bean.m_id = idx;
                bean.m_time = "2022-1-1 19:54:45 223";
                bean.m_name = "变电站1.设备1.Ua";
                bean.m_fault_value = "252.1V";
                bean.SetValue(1);
                alarm_data_array.add(bean);
            };
        }
    };

    //报表数据模块
    public class ReportModule{
        public ReportDataQuery m_report_data_query = new ReportDataQuery();
        public ArrayList<ReportDataItemBean> report_data_list = new ArrayList<ReportDataItemBean>();
        public int AddReportDataQueryRecord(String start_time, String end_time, int station_id, int device_id, int item_id, String station_name, String device_name)
        {

            if (m_report_data_query == null)
            {
                return 1;
            }

            m_report_data_query.device_id = device_id;
            m_report_data_query.station_id = station_id;
            m_report_data_query.m_start_time = start_time;
            m_report_data_query.m_end_time = end_time;
            m_report_data_query.analog_list.clear();
            m_report_data_query.analog_list.add(item_id);
            m_report_data_query.station_name = station_name;
            m_report_data_query.device_name = device_name;
            m_report_data_query.new_query = 1;
            return 0;
        }
        public ArrayList<ReportDataItemBean> GetReportDataArray()
        {
            if (m_report_data_query == null)
            {
                return null;
            }
            return m_report_data_query.report_data_array;
        }
    }


    public int AddReportDataQueryRecord(String start_time, String end_time, int station_id, int device_id, int item_id)
    {
        if (m_report_module == null)
        {
            return 1;
        }
        PwrStation the_station = GetPwrStation(station_id);
        if (the_station == null)
        {
            return 1;
        }
        PwrDevice the_device = the_station.GetPwrDevice(device_id);
        if (the_device == null)
        {
            return 1;
        }
        return m_report_module.AddReportDataQueryRecord(start_time, end_time, station_id, device_id, item_id, the_station.getM_Name(), the_device.getM_Name());
    }

    public PwrDataModule.ReportDataQuery IsNewReportQueryExist()
    {
        if (m_report_module == null)
        {
            return null;
        }
        if (m_report_module.m_report_data_query.new_query == 1)
        {
            m_report_module.m_report_data_query.new_query = 0;
            return m_report_module.m_report_data_query;
        }
        return null;
    }
    //----------------------------------------------------------------------------
    //报表数据模块
    public class AlarmRecordDataModule{
        public AlarmDataQuery m_alarm_data_query = new AlarmDataQuery();
        public ArrayList<AlarmRecordItemBean> report_data_list = new ArrayList<AlarmRecordItemBean>();
        public int AddAlarmReportDataRecord(String start_time, String end_time, int station_id, int device_id, int item_id, String station_name, String device_name)
        {

            if (m_alarm_data_query == null)
            {
                return 1;
            }

            m_alarm_data_query.m_device_id = device_id;
            m_alarm_data_query.m_station_id = station_id;
            m_alarm_data_query.m_start_time = start_time;
            m_alarm_data_query.m_end_time = end_time;
            m_alarm_data_query.analog_list.clear();
            m_alarm_data_query.analog_list.add(item_id);
            m_alarm_data_query.station_name = station_name;
            m_alarm_data_query.device_name = device_name;
            m_alarm_data_query.new_query = 1;
            return 0;
        }
        public ArrayList<AlarmRecordItemBean> GetAlarmRecoordDataArray()
        {
            if (m_alarm_data_query == null)
            {
                return null;
            }
            return m_alarm_data_query.alarm_data_array;
        }
    }



    //报表数据查询
    public ReportDataQuery m_report_data_query = new ReportDataQuery();
    public ReportModule m_report_module = new ReportModule();
    public AlarmRecordDataModule m_alarm_module = new AlarmRecordDataModule();
    public EventRecordDataModule m_event_module = new EventRecordDataModule();

    PwrStationSet m_StationSet = new PwrStationSet();
    PwrStation m_CurrentStation = null;
    public void SetCurrentStationNode(PwrStation Station)
    {
        m_CurrentStation = Station;
    }

    public PwrStation GetCurrentStationNode()
    {
        return m_CurrentStation;
    }

    public String m_name;
    int m_CurrentStationID = 1;

    public void SetCurrentStationID(int StationID)
    {
        m_CurrentStationID = StationID;
    }

    int GetCurrentStationID()
    {
        return m_CurrentStationID;
    }

    public PwrStationSet GetPwrStationSet() {
        return m_StationSet;
    }

    //获取查询到的报表数据队列
    public ArrayList<ReportDataItemBean> GetReportDataArray()
    {
        if (m_report_module == null)
        {
            return null;
        }
        return m_report_module.GetReportDataArray();
    }
    //获取查询到的报警数据队列
    public ArrayList<EventRecordItemBean> GetEventRecordDataArray()
    {
        if (m_event_module == null)
        {
            return null;
        }
        return m_event_module.GetEventRecordDataArray();
    }

    //获取查询到的报警数据队列
    public ArrayList<AlarmRecordItemBean> GetAlarmRecordDataArray()
    {
        if (m_alarm_module == null)
        {
            return null;
        }
        return m_alarm_module.GetAlarmRecoordDataArray();
    }
    public PwrStation GetCurrentStation()
    {
        PwrStationSet Set = GetPwrStationSet();
        if (Set == null)
        {
            return null;
        }
        return Set.GetPwrStation(GetCurrentStationID() );
    }

    //获取第一个站节点
    public PwrStation GetFirstStation()
    {
        PwrStationSet Set = GetPwrStationSet();
        if (Set == null)
        {
            return null;
        }
        return Set.GetHead();
    }


    public PwrStation GetPwrStationByID(int ID)
    {
        PwrStationSet Set = GetPwrStationSet();
        if (Set == null)
        {
            return null;
        }
        return Set.GetPwrStation(ID);
    }
    /*
     *在内存中创建一个站节点
     *StationID: 站ID
     *Name：站名称
     *
     * */
    //曲线记录集合
    CurveRecordSet m_CurveRecordSet = new CurveRecordSet();
    EventRecordSet m_EventRecordSet = new EventRecordSet();


    private static class PwrDataModuleSingleton {
        private static final PwrDataModule INSTANCE = new PwrDataModule();

    }

    public static PwrDataModule getInstance() {
        return PwrDataModuleSingleton.INSTANCE;
    }

    public void initData() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Exception {
                // 这里面就是子线程 你的数据访问逻辑放在这里
                InputStream input = null;

                try {
                    input = UIUtils.getContext().getAssets().open("data.json");
                    Scanner scanner = new Scanner(input, "UTF-8").useDelimiter("\\A");
                    String json = "";
                    if (scanner.hasNext()) {
                        json = scanner.next();
                    }
                    emitter.onNext(json);
                    CreateJxtObject();

                } catch (Exception e) {
                    e.printStackTrace();
                    emitter.onError(e);
                } finally {
                    if (input != null)
                        input.close();
                }
            }
        }).compose(RxSchedulersHelper.applyIoTransformer()).subscribe(new ProgressObserver<String>(false, null) {
            @Override
            public void _onNext(String json) {
                //这里是主线程，在这里获取数据
                //initData(json);<---此处用于测试站
            }

            @Override
            public void _onError(String errorMessage, int errorCode) {
                super._onError(errorMessage, errorCode);
            }
        });

    }


    public void initData(String json) {
        SiteBean siteData = JSONObject.parseObject(json, SiteBean.class);
        List<SiteStationBean> sections = siteData.stations;
        PwrDevice thePwrDevice = null;
        DataTeam theDataTeam = null;
        for (SiteStationBean section : sections)
        {
            CreateStation(section.id, section.name);
            List<StationDeviceBean> devices = section.devices;
            for (StationDeviceBean device : devices)
            {
                thePwrDevice = CreateDevice(section.stationId, device.name, device.id);
                if (thePwrDevice != null)
                {
                    List<DeviceValueBean> values = device.values;
                    if (values != null)
                    {
                        int valueCount = values.size();
                        if (valueCount > 0)
                        {
                            int index = 0;
                            //创建数据组数据
                            theDataTeam = thePwrDevice.CreateDataTeamData(DataTeam.DATA_TYPE_ANALOG, valueCount);
                            for (DeviceValueBean value : values)
                            {
                                theDataTeam.SetName(index, value.name);
                                theDataTeam.SetUnit(index, value.unit);
                                index++;
                            }
                        }
                    }

                }
            }
        }
        ArrayList<PwrStation> pwrStations = PwrDataModule.getInstance().GetAllStationList();
        for (PwrStation pwrStation : pwrStations) {
            Logger.d("pwrStation" + pwrStation.getM_ID() + pwrStation.getM_Name());
        }
    }
    //创建站点
    PwrStation CreateStation(int StationID, String Name)
    {
        PwrStationSet Set = GetPwrStationSet();
        PwrStation Station = null;
        if (Set == null) {
            return null;
        }
        Station = Set.GetPwrStation(StationID);
        if (Station == null)
        {
            PwrStation NewStation = new PwrStation();
            NewStation.SetID(StationID);
            NewStation.SetName(Name);
            if (NewStation != null)
            {
                Set.AppendAStation(NewStation);
                return NewStation;
            }
            return null;
        }
        else
        {
            return Station;
        }
    }

    //创建设备
    /*
     *
     * 在某个站下面创建一个设备
     * StationID:站ID
     * Name：设备名称
     * DeviceID:设备ID
     *
     *
     * */
    PwrDevice CreateDevice(int StationID, String DeviceName, int DeviceID)
    {
        PwrStationSet Set = GetPwrStationSet();
        PwrStation Station = null;
        if (Set == null) {
            return null;
        }
        Station = Set.GetPwrStation(StationID);
        if (Station == null)
        {
            return null;
        }
        PwrDevice Device = new PwrDevice();
        if (Device == null)
        {
            return null;
        }
        Device.SetID(DeviceID);
        Device.SetName(DeviceName);
        if (Station.AddDevice(Device) == 1)
        {
            Device = null;
        };
        return Device;
    }
    //创建数据组
    int CreateDataTeam(int StationID, int DeviceID, byte Type, int DataCount)
    {
        PwrDevice Device = GetPwrDevice(StationID, DeviceID);
        DataTeam theDataTeam;
        if (Device == null)
        {
            return 1;
        }
        theDataTeam = Device.GetDataTeam(Type);
        if (theDataTeam == null)
        {
            return 1;
        }
        return theDataTeam.CreateTeamData(Type, DataCount);
    }
    /*
     *获取站节点对象
     *StationID：站ID
     *
     *
     * */

    PwrStation GetPwrStation(int StationID) {
        PwrStationSet Set = GetPwrStationSet();
        if (Set == null) {
            return null;
        }
        return Set.GetPwrStation(StationID);
    }

    /*
    获取设备
    int StationID,:站ID
    int DeviceID:设备ID
    */
    PwrDevice GetPwrDevice(int StationID, int DeviceID) {
        PwrStation Station = GetPwrStation(StationID);
        if (Station == null) {
            return null;
        }
        return Station.GetPwrDevice(DeviceID);
    }
    //获取曲线数据集合
    CurveRecordSet GetCurveRecordSet() {
        return m_CurveRecordSet;
    }

    EventRecordSet GetEventRecordSet() {
        return m_EventRecordSet;
    }

    //获取站列表
    public    ArrayList<PwrStation> GetAllStationList() {
        ArrayList<PwrStation> list = new ArrayList<PwrStation>();
        PwrStationSet Set = GetPwrStationSet();
        if (Set == null) {
            return null;
        }
        PwrStation head = Set.GetHead();
        while (head != null) {
            list.add(head);
            head = head.GetNext();
        }
        return list;
    }

    //查询站下面的设备列表
    public   ArrayList<PwrDevice> GetDeviceList(int StationID) {
        PwrDevice Device = null;
        PwrStation Station = GetPwrStation(StationID);
        if (Station == null) {
            return null;
        }
        Device = Station.GetFirstDevice();
        ArrayList<PwrDevice> List = new ArrayList<PwrDevice>();
        while (Device != null) {
            List.add(Device);
            Device = Device.GetNext();
        }
        return List;
    }

    //查询某一设备下的模拟量数据
    public  ArrayList<AnalogDataItem> GetAnalogDataList(int StationID, int DeviceID) {
        PwrDevice Device = GetPwrDevice(StationID, DeviceID);
        ArrayList<AnalogDataItem> List = null;
        if (Device == null) {
            return null;
        }
        DataTeam Team = Device.GetFristDataTeam();
        while (Team != null) {
            if (Team.m_Type == DataTeam.DATA_TYPE_ANALOG) {
                List = new ArrayList<AnalogDataItem>();
                for (int idx = 0; idx < Team.m_ItemCount; idx++) {
                    List.add(Team.m_AnalogItem[idx]);
                }
                return List;
            }
            Team = Team.GetNext();
        }
        return null;
    }

    //查询某一设备下的数字量数据
    public    ArrayList<DigitalDataItem> GetDigitalDataList(int StationID, int DeviceID) {
        PwrDevice Device = GetPwrDevice(StationID, DeviceID);
        ArrayList<DigitalDataItem> List = null;
        if (Device == null) {
            return null;
        }
        DataTeam Team = Device.GetFristDataTeam();
        while (Team != null) {
            if (Team.m_Type == DataTeam.DATA_TYPE_SIGNAL) {
                List = new ArrayList<DigitalDataItem>();
                for (int idx = 0; idx < Team.m_ItemCount; idx++) {
                    List.add(Team.m_DigitalItem[idx]);
                }
                return List;
            }
            Team = Team.GetNext();
        }
        return null;
    }

    //Bean-----------------------------------------------------------
    public List<SiteStationBean> GetStaionBeanList()
    {
        List<SiteStationBean> StationList = new ArrayList<SiteStationBean>();
        PwrStationSet Set = GetPwrStationSet();
        if (Set == null)
        {
            return null;
        }
        PwrStation head = Set.GetHead();
        while (head != null)
        {
            StationList.add(head.GetStationBean());
            head = head.GetNext();
        }
        return StationList;
    }

    PwrStation CreateStation(SiteStationBean bean)
    {
        PwrStation TheStation = null;
        if (bean == null)
        {
            return null;
        }
        TheStation = CreateStation(Integer.valueOf(bean.stationId), bean.company_name);
        if (TheStation == null)
        {
            return null;
        }
        TheStation.SetStationBean(bean);
        return TheStation;
    }
    public int CreateStationByBeanList(List<SiteStationBean> beans)
    {
        PwrStationSet Set = GetPwrStationSet();
        PwrStation TheStation = null;
        if (Set == null) {
            return 1;
        }
        Set.Release();
        for (SiteStationBean TheBean:beans)
        {
            TheStation = CreateStation(TheBean);
        }
        return 0;
    }

    public int CreateAllPowerDeviceOfAStation(int  StationID, List<PwrDeviceBean> Beans)
    {
        PwrStation Station = GetPwrStation(StationID);
        if (Station == null)
        {
            return 1;
        }
        return Station.CreateAllPowerDeviceOfAStation(Beans);
    }

    public int GetStationDeviceCount(int  StationID)
    {
        PwrStation Station = GetPwrStation(StationID);
        if (Station == null)
        {
            return 1;
        }
        return Station.GetDeviceCount();
    }

    public List<PwrDeviceBean> GetStationDeviceBeanList(int  StationID)
    {
        PwrStation Station = GetPwrStation(StationID);
        if (Station == null)
        {
            return null;
        }
        return Station.GetDeviceBeanList();
    }


    public int IsAllDeviceBeLoaded()
    {
        PwrStation station = GetFirstStation();
        while (station != null)
        {
            if (station.IsAllDeviceBeLoaded() == 0)
            {
                return 0;
            }
            station = station.GetNext();
        }
        return 1;
    }
    public int IsAllDataPointBeLoaded()
    {
        if (IsDataPointLoaded() == 1)
        {
            return 1;
        }
        PwrStation station = GetFirstStation();
        while (station != null)
        {
            if (station.IsAllPointBeLoaded() == 0)
            {
                return 0;
            }
            station = station.GetNext();
        }
        return 1;
    }

    public AnalogDataItem GetDataItem(DataLocation location)
    {
        if (location == null)
        {
            return null;
        }
        PwrStation station = GetPwrStation(location.m_StationID);
        if (station == null)
        {
            return null;
        }
        PwrDevice device = station.GetPwrDeviceByIndex(location.m_DataTeamIndex);
        if (device == null)
        {
            return null;
        }
        return device.GetAnalogDataItem(location.m_DataIndex);
    }



    /*=============================================================================================================================================================================================
 //
 // ---------------------------------------------------------------------------------------接线图----------------------------------------------------------------------
 //
 //
 //
 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    public int m_UserID = 3;
    ////接线图相关的...
    JxtXmlPara m_Jxt = null;
    public int m_JxtSign = -1;
    String m_BasePath = "";
    String m_FileDir;
    int m_DownJxtSign = 0;
    public int m_JxtPos=0;

    public byte m_State[][];
    public float m_Value[][];
    int m_Width = 2160;

    public int SetJxtPageIndex(int index)
    {
        if (m_Jxt == null)
        {
            return 1;
        }
        if (m_Jxt.m_Qjpmc_XmlCnfg == null)
        {
            return 1;
        }
        if ( (index >= 0) && (index < m_Jxt.m_Qjpmc_XmlCnfg.m_PageNum) )
        {
            m_JxtPos = index;
            return 0;
        }
        return 1;
    }

    public int CreateJxtObject()
    {
        m_Jxt=new JxtXmlPara();
        String Path1 = "Chart";
        String Path2 = "User_" + Integer.toString(m_UserID);
        String fileName = "Chart.xml";
        //m_BasePath = m_FileDir + "";
        m_Jxt.m_ChartPath = m_BasePath + "/" + Path1 + "/" + Path2 + "/" + fileName; //文件存储路径

        //m_JxtSign = m_Jxt.ReadJxtXml();
        if (m_JxtSign == 0)
        {
/*
            ShowFragment(0);
            restartBotton();
            tv_jxt.setTextColor(0xff333333);
            tv_top_title.setText("主接线图");
*/
        }
        else
        {
            DownLoadJxt();
        }
        return 0;
    }

    public void SetBasePath(String Path)
    {
        m_BasePath = Path;
    }
    //下载接线图
    public int DownLoadJxt()
    {
        //String urlStr = "http://39.108.172.55/Chart/User_" + Integer.toString(m_UserID) + "/Chart.xml";
        //String urlStr = "http://120.76.112.228/Chart/User_" + Integer.toString(m_UserID) + "/Chart.xml";
        //String urlStr = "http://39.108.120.59/Chart1.xml";
        //String urlStr = "http://39.108.120.59/Chart_PT.xml";
        String urlStr = "http://114.116.126.61/Chart_PT.xml";
        //http://114.116.126.61/Chart_PT.xml
        String Path1 = "Chart";
        String Path2 = "User_" + Integer.toString(m_UserID);
        String fileName = "Chart.xml";

        //本地路径:
        //m_BasePath = this.getApplicationContext().getFilesDir() + "";
        String pathName = m_BasePath + "/" + Path1 + "/" + Path2 + "/";//文件存储路径


        try {
            File filePath = new File(pathName);
            if (!filePath.exists())
            {

                if (filePath.mkdirs())
                {
                }
                else
                {
                    return 0;

                }

            }

            pathName = m_BasePath + "/" + Path1 + "/" + Path2 + "/" + fileName;
            File file = new File(pathName);
            if (file.exists()) {
                file.delete();
            }

            // 构造URL
            URL url = new URL(urlStr);
            // 打开连接

            HttpURLConnection con = (HttpURLConnection)url.openConnection();
/*
            con.setReadTimeout(5000);

            con.setConnectTimeout(10000);

            con.setDoInput(true);

            con.setDoOutput(true);

            con.connect();
*/

            if( con.getResponseCode() != HttpURLConnection.HTTP_OK)
            {
                return 1;
            }
            /*
            URLConnection con = url.openConnection();
            */// 输入流
            InputStream is = con.getInputStream();

            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            OutputStream os = new FileOutputStream(pathName);
            // 开始读取

            try
            {
                while ((len = is.read(bs)) != -1) {
                    os.write(bs, 0, len);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            // 完毕，关闭所有链接
            os.close();
            is.close();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        m_DownJxtSign = 1;
        m_JxtSign = m_Jxt.ReadJxtXml();
        InitDataBuff();

        if (m_JxtSign == 0)
        {

        }
        return 0;
    }

    public PwrDiagramObject GetPwrDiagramObject()
    {
        if (m_Jxt == null)
        {
            return null;
        }
        return m_Jxt.m_Qjpmc_XmlCnfg;
    }

    public void InitDataBuff()
    {
        PwrDiagramObject m_JxtCnfg = null;
        if (m_Jxt == null)
        {
            return;
        }
        m_JxtCnfg = m_Jxt.m_Qjpmc_XmlCnfg;
        if(m_JxtCnfg == null)
        {
            return;
        }
        m_State=new byte[m_JxtCnfg.m_PageNum][];
        m_Value=new float[m_JxtCnfg.m_PageNum][];

        for(int i=0;i<m_JxtCnfg.m_PageNum;i++)
        {
            PwrDiagramObject.Page JxtPage = m_JxtCnfg.m_Page.get(i);
            if(JxtPage == null)
            {
                continue;
            }
            if(JxtPage.m_StateComptNum>0)
            {
                m_State[i] = new byte[JxtPage.m_StateComptNum];
                JxtPage.m_DigitalValues = new DataLocation[JxtPage.m_StateComptNum];
                StateCompt StatComponent = null;
                for(int j = 0; j < JxtPage.m_StateComptNum; j++)
                {
                    StatComponent = JxtPage.m_StateComptList.get(j);
                    if (StatComponent != null)
                    {
                        DataLocation dataLocation = new DataLocation();
                        dataLocation.m_StationID = StatComponent.m_StationID;//站ID
                        dataLocation.m_DataTeamID = StatComponent.m_GroupIdx;//设备ID
                        dataLocation.m_DataIndex = StatComponent.m_DataIdx;//数据索引
                        JxtPage.m_DigitalValues[j] = dataLocation;
                    }
                    m_State[i][j] = 2;
                }
            }

            if(JxtPage.m_ValueComptNum>0)
            {
                m_Value[i] = new float[JxtPage.m_ValueComptNum];
                PwrDiagramObject.ValueCompt ValueComponent = null;
                JxtPage.m_AnaloglValues = new DataLocation[JxtPage.m_ValueComptNum];
                for(int j = 0; j < JxtPage.m_ValueComptNum; j++)
                {
                    ValueComponent = JxtPage.m_ValueCompt.get(j);
                    if (ValueComponent != null)
                    {
                        DataLocation dataLocation = new DataLocation();
                        dataLocation.m_StationID = ValueComponent.m_StationID;//站ID
                        dataLocation.m_DataTeamID = ValueComponent.m_GroupIdx;//设备ID
                        dataLocation.m_DataIndex = ValueComponent.m_DataIdx;//数据索引
                        JxtPage.m_AnaloglValues[j] = dataLocation;

                    }
                    m_Value[i][j] = 0;
                }
            }
        }
    }

    public void LinkDataItemToJXT()
    {
        PwrDiagramObject m_JxtCnfg = null;
        if (m_Jxt == null)
        {
            return;
        }
        m_JxtCnfg = m_Jxt.m_Qjpmc_XmlCnfg;
        if(m_JxtCnfg == null)
        {
            return;
        }

        RTDataEntrance entrance = null;
        for(int i = 0; i < m_JxtCnfg.m_PageNum; i++) {
            entrance = new RTDataEntrance();
            if (entrance != null) {
                m_RtDataEntranceMng.m_JxtRtDataEntranceList.add(entrance);
            }
            PwrDiagramObject.Page JxtPage = m_JxtCnfg.m_Page.get(i);
            if (JxtPage == null) {
                continue;
            }
            if (JxtPage.m_StateComptNum > 0) {
                StateCompt StatComponent = null;
                for (int j = 0; j < JxtPage.m_StateComptNum; j++) {
                    StatComponent = JxtPage.m_StateComptList.get(j);
                    if (StatComponent != null) {
                        DataLocation dataLocation = StatComponent.GetDataLocation();

                        AnalogDataItem item = GetDataItem(dataLocation);
                        StatComponent.m_DataEntrance.SetAnalogDataItem(item);

                        //设定数据入口表
                        if (item != null) {
                            entrance.AddNode(item.m_TagName, item);
                        }
                    }
                    m_State[i][j] = 2;
                }
            }

            if (JxtPage.m_ValueComptNum > 0) {
                m_Value[i] = new float[JxtPage.m_ValueComptNum];
                PwrDiagramObject.ValueCompt ValueComponent = null;
                JxtPage.m_AnaloglValues = new DataLocation[JxtPage.m_ValueComptNum];
                for (int j = 0; j < JxtPage.m_ValueComptNum; j++) {
                    ValueComponent = JxtPage.m_ValueCompt.get(j);
                    if (ValueComponent != null) {

                        DataLocation dataLocation = ValueComponent.GetDataLocation();

                        AnalogDataItem item = GetDataItem(dataLocation);
                        if (ValueComponent.m_DataEntrance != null) {
                            ValueComponent.m_DataEntrance.SetAnalogDataItem(item);
                        }
                        //设定数据入口表
                        if (item != null) {
                            entrance.AddNode(item.m_TagName, item);
                        }

                        //JxtPage.m_AnaloglValues[j] = dataLocation;

                    }
                    m_Value[i][j] = 0;
                }
            }
            if (entrance != null)
            {
                entrance.GeneratePointCodeList();
            }
        }
    }

    protected void DrawButton(PwrDiagramObject.NormalBtn BtnTemp, Canvas canvas)
    {
        PwrDiagramObject m_JxtCnfg = null;
        if (m_Jxt == null)
        {
            m_JxtCnfg = m_Jxt.m_Qjpmc_XmlCnfg;
        }
        if(m_JxtCnfg == null)
        {
            return;
        }
        if(BtnTemp==null) return;

        PointF StartPnt=GetPoint(BtnTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(BtnTemp.m_EndPoint);
        PointToView(canvas, EndPnt);

        int LineClr=GetColor(BtnTemp.m_TextColor);
        int FillClr=GetColor(BtnTemp.m_BackColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);//空心


        RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);
        canvas.drawRect(rect, paint);

        paint.setColor(FillClr);
        paint.setStyle(Paint.Style.FILL);//实心
        RectF FillRect = new RectF(StartPnt.x+5, StartPnt.y+5, EndPnt.x-5, EndPnt.y-5);
        canvas.drawRect(rect, paint);

        paint.setColor(LineClr);
        paint.setStrokeWidth(3);

        int Width=canvas.getWidth();
        int WidthEx=m_JxtCnfg.m_ScreenWidth;
        int FontSize = BtnTemp.m_FontSize * Width *2 / WidthEx;

        paint.setTextSize(FontSize);
        paint.setTextAlign(Paint.Align.CENTER);//中对齐
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();

        float baseline = (StartPnt.y+EndPnt.y - fontMetrics.bottom - fontMetrics.top) / 2;
        canvas.drawText(BtnTemp.m_BtnConText, (StartPnt.x+EndPnt.x)/2, baseline, paint);
    }

    protected void DrawValueCompt(PwrDiagramObject.ValueCompt ValueComptTemp, Canvas canvas, int Pos)
    {
        PwrDiagramObject m_JxtCnfg = null;
        if (m_Jxt != null)
        {
            m_JxtCnfg = m_Jxt.m_Qjpmc_XmlCnfg;
        }
        if(m_JxtCnfg == null)
        {
            return;
        }
        if(ValueComptTemp==null) return;

        PointF StartPnt=GetPoint(ValueComptTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(ValueComptTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int TextClr=GetColor(ValueComptTemp.m_TextColor);
        int BackClr=GetColor(ValueComptTemp.m_BackColor);

        Paint paint = new Paint();
        paint.setColor(TextClr);
        paint.setStrokeWidth(3);

        int Width=canvas.getWidth();
        int WidthEx=m_JxtCnfg.m_ScreenWidth;
        int FontSize = ValueComptTemp.m_FontSize * Width *2 / WidthEx;

        paint.setTextSize(FontSize);

        String strFormat=String.format("%%.%df",ValueComptTemp.m_DecimalPlace);

        String strValue= new String("");


        //改成通过data entrance 访问, 2022年7月20日
        if (ValueComptTemp.m_DataEntrance != null)
        {
            AnalogDataItem Item = ValueComptTemp.m_DataEntrance.GetAnalogDataItem();
            if (Item != null)
            {
                strValue = String.format(strFormat, Item.m_value);
            }
            else
            {
                strValue = "--";
            }
        }
/*
        if (m_Value[m_JxtPos] != null) {
            strValue = String.format(strFormat, m_Value[m_JxtPos][Pos]);
        }

 */
        strValue+=" ";
        strValue+=ValueComptTemp.m_Unit;

        if(ValueComptTemp.m_AlignType==3) {
            paint.setTextAlign(Paint.Align.RIGHT);//右对齐
            canvas.drawText(strValue, EndPnt.x, StartPnt.y, paint);
        }
        else if(ValueComptTemp.m_AlignType==1) {
            paint.setTextAlign(Paint.Align.LEFT);//左对齐
            canvas.drawText(strValue, StartPnt.x, StartPnt.y, paint);
        }
        else if(ValueComptTemp.m_AlignType==2) {
            paint.setTextAlign(Paint.Align.CENTER);//中对齐
            Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();

            float baseline = (StartPnt.y+EndPnt.y - fontMetrics.bottom - fontMetrics.top) / 2;
            canvas.drawText(strValue, (StartPnt.x + EndPnt.x) / 2, baseline, paint);
        }
    }

    protected void DrawStateCompt(StateCompt StateComptTemp, Canvas canvas, int Pos)
    {
        if(StateComptTemp.m_StateNum<=0) return;
        if(StateComptTemp.m_DefaultState>=StateComptTemp.m_StateNum) return;
        PwrDiagramObject.Element ElementTemp=null;

        if(StateComptTemp.m_ShowType == 1)
        {
            ElementTemp = StateComptTemp.m_Element.get(m_State[m_JxtPos][Pos]);
        }
        else if(StateComptTemp.m_ShowType==2)
        {
            if (m_State[m_JxtPos] != null)
            {
                if(m_State[m_JxtPos][Pos]==0)
                {
                    ElementTemp=StateComptTemp.m_Element.get(0);
                }
                else if(m_State[m_JxtPos][Pos]==2)
                {
                    ElementTemp=StateComptTemp.m_Element.get(StateComptTemp.m_StateNum-1);
                }
                else
                {
                    ElementTemp=StateComptTemp.m_Element.get(StateComptTemp.m_ShowPos);
                    StateComptTemp.m_ShowPos++;
                    if ( StateComptTemp.m_ShowPos>=StateComptTemp.m_StateNum - 1)
                    {
                        StateComptTemp.m_ShowPos = 1;
                    }
                }
            }

        }

        DrawElement(ElementTemp,canvas);
    }

    protected void DrawElement(PwrDiagramObject.Element ElementTemp, Canvas canvas)
    {
        for(int i=0;i<ElementTemp.m_RectNum && i<ElementTemp.m_Rect.size();i++) {
            PwrDiagramObject.Rect RectTemp= ElementTemp.m_Rect.get(i);
            String StartPnt=RectTemp.m_StartPoint;
            String EndPnt=RectTemp.m_EndPoint;
            RectTemp.m_StartPoint=GetElementPoint(ElementTemp,RectTemp.m_StartPoint);
            RectTemp.m_EndPoint=GetElementPoint(ElementTemp,RectTemp.m_EndPoint);
            DrawRect(RectTemp, canvas);
            RectTemp.m_StartPoint = StartPnt;
            RectTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_CircleNum && i<ElementTemp.m_Circle.size();i++) {
            PwrDiagramObject.Circle CircleTemp=ElementTemp.m_Circle.get(i);
            String StartPnt=CircleTemp.m_StartPoint;
            String EndPnt=CircleTemp.m_EndPoint;
            CircleTemp.m_StartPoint=GetElementPoint(ElementTemp,CircleTemp.m_StartPoint);
            CircleTemp.m_EndPoint=GetElementPoint(ElementTemp,CircleTemp.m_EndPoint);
            DrawCircle(CircleTemp, canvas);
            CircleTemp.m_StartPoint = StartPnt;
            CircleTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_ArcNum && i<ElementTemp.m_Arc.size();i++) {
            PwrDiagramObject.Arc ArcTemp= ElementTemp.m_Arc.get(i);
            String StartPnt=ArcTemp.m_StartPoint;
            String EndPnt=ArcTemp.m_EndPoint;
            ArcTemp.m_StartPoint=GetElementPoint(ElementTemp,ArcTemp.m_StartPoint);
            ArcTemp.m_EndPoint=GetElementPoint(ElementTemp,ArcTemp.m_EndPoint);
            DrawArc(ArcTemp, canvas);
            ArcTemp.m_StartPoint = StartPnt;
            ArcTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_LineNum && i<ElementTemp.m_Line.size();i++) {
            PwrDiagramObject.Line LineTemp=ElementTemp.m_Line.get(i);
            String StartPnt=LineTemp.m_StartPoint;
            String EndPnt=LineTemp.m_EndPoint;
            LineTemp.m_StartPoint=GetElementPoint(ElementTemp,LineTemp.m_StartPoint);
            LineTemp.m_EndPoint=GetElementPoint(ElementTemp,LineTemp.m_EndPoint);
            DrawLine(LineTemp, canvas);
            LineTemp.m_StartPoint = StartPnt;
            LineTemp.m_EndPoint = EndPnt;
        }
        for(int i=0;i<ElementTemp.m_TextNum && i<ElementTemp.m_Text.size();i++) {
            PwrDiagramObject.Text TextTemp=ElementTemp.m_Text.get(i);
            String StartPnt=TextTemp.m_StartPoint;
            String EndPnt=TextTemp.m_EndPoint;
            TextTemp.m_StartPoint=GetElementPoint(ElementTemp,TextTemp.m_StartPoint);
            TextTemp.m_EndPoint=GetElementPoint(ElementTemp,TextTemp.m_EndPoint);
            DrawText(TextTemp, canvas);
            TextTemp.m_StartPoint = StartPnt;
            TextTemp.m_EndPoint = EndPnt;
        }
    }

    protected String GetElementPoint(PwrDiagramObject.Element ElementTemp, String strPoint)
    {
        PointF Pnt=new PointF(-1,-1);
        String[] strPos = strPoint.split(",");
        if(strPos.length!=2) return null;
        Pnt.x = Float.parseFloat(strPos[0]);
        Pnt.y = Float.parseFloat(strPos[1]);

        PointF PntElementS=new PointF(-1,-1);
        String[] strPosElementS = ElementTemp.m_StartPoint.split(",");
        if(strPosElementS.length!=2) return null;
        PntElementS.x = Float.parseFloat(strPosElementS[0]);
        PntElementS.y = Float.parseFloat(strPosElementS[1]);

        PointF PntElementE=new PointF(-1,-1);
        String[] strPosElementE =  ElementTemp.m_EndPoint.split(",");
        if(strPosElementE.length!=2) return null;
        PntElementE.x = Float.parseFloat(strPosElementE[0]);
        PntElementE.y = Float.parseFloat(strPosElementE[1]);

        float x = Pnt.x * (PntElementE.x-PntElementS.x)/1000 + PntElementS.x;
        float y = Pnt.y * (PntElementE.y-PntElementS.y)/1000 + PntElementS.y;

        String strResult="";
        strResult+=Float.toString(x);
        strResult+=",";
        strResult+=Float.toString(y);
        return strResult;
    }


    protected void DrawRect(PwrDiagramObject.Rect RectTemp, Canvas canvas)
    {
        if(RectTemp==null) return;

        PointF StartPnt=GetPoint(RectTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(RectTemp.m_EndPoint);
        PointToView(canvas, EndPnt);

        int LineClr=GetColor(RectTemp.m_LineColor);
        int FillClr=GetColor(RectTemp.m_FillColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(RectTemp.m_LineWidth);
        if(RectTemp.m_FillSign==0)
            paint.setStyle(Paint.Style.STROKE);//空心
        else if(LineClr==FillClr)
            paint.setStyle(Paint.Style.FILL);//实心
        else
            paint.setStyle(Paint.Style.STROKE);//空心

        RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);
        canvas.drawRect(rect, paint);

        if(RectTemp.m_FillSign==1 && FillClr!=LineClr) {
            paint.setColor(FillClr);
            paint.setStyle(Paint.Style.FILL);//实心
            RectF FillRect = new RectF(StartPnt.x+RectTemp.m_LineWidth, StartPnt.y+RectTemp.m_LineWidth, EndPnt.x-RectTemp.m_LineWidth, EndPnt.y-RectTemp.m_LineWidth);
            canvas.drawRect(rect, paint);
        }
    }

    protected void DrawCircle(PwrDiagramObject.Circle CircleTemp, Canvas canvas)
    {
        if(CircleTemp==null) return;

        PointF StartPnt=GetPoint(CircleTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(CircleTemp.m_EndPoint);
        PointToView(canvas, EndPnt);

        int LineClr=GetColor(CircleTemp.m_LineColor);
        int FillClr=GetColor(CircleTemp.m_FillColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(CircleTemp.m_LineWidth);
        if(CircleTemp.m_FillSign==0)
            paint.setStyle(Paint.Style.STROKE);//空心
        else if(LineClr==FillClr)
            paint.setStyle(Paint.Style.FILL);//实心
        else
            paint.setStyle(Paint.Style.STROKE);//空心

        canvas.drawCircle((StartPnt.x + EndPnt.x) / 2, (StartPnt.y + EndPnt.y) / 2, Math.abs(EndPnt.y - StartPnt.y) / 2, paint);

        if(CircleTemp.m_FillSign==1 && FillClr!=LineClr) {
            paint.setColor(FillClr);
            paint.setStyle(Paint.Style.FILL);//实心
            StartPnt.x+=CircleTemp.m_LineWidth;
            StartPnt.y+=CircleTemp.m_LineWidth;
            EndPnt.x-=CircleTemp.m_LineWidth;
            EndPnt.y-=CircleTemp.m_LineWidth;
            canvas.drawCircle((StartPnt.x + EndPnt.x) / 2, (StartPnt.y + EndPnt.y) / 2, Math.abs(EndPnt.y - StartPnt.y) / 2 + 1, paint);
        }
    }

    protected void DrawLine(PwrDiagramObject.Line LineTemp, Canvas canvas)
    {
        if(LineTemp==null) return;

        PointF StartPnt=GetPoint(LineTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(LineTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int LineClr=GetColor(LineTemp.m_LineColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(LineTemp.m_LineWidth);

        canvas.drawLine(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y, paint);
    }

    protected void DrawArc(PwrDiagramObject.Arc ArcTemp, Canvas canvas)
    {
        if(ArcTemp==null) return;

        PointF StartPnt=GetPoint(ArcTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(ArcTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int LineClr=GetColor(ArcTemp.m_LineColor);

        Paint paint = new Paint();
        paint.setColor(LineClr);
        paint.setStrokeWidth(ArcTemp.m_LineWidth);
        paint.setStyle(Paint.Style.STROKE);

        RectF rect = new RectF(StartPnt.x, StartPnt.y, EndPnt.x, EndPnt.y);

        canvas.drawArc(rect, //弧线所使用的矩形区域大小
                ArcTemp.m_StartAngle,  //开始角度
                ArcTemp.m_EndAngle-ArcTemp.m_StartAngle, //扫过的角度
                false, //是否使用中心
                paint);
    }


    protected void DrawText(PwrDiagramObject.Text TextTemp, Canvas canvas)
    {
        if(TextTemp==null) return;

        PointF StartPnt=GetPoint(TextTemp.m_StartPoint);
        PointToView(canvas,StartPnt);
        PointF EndPnt=GetPoint(TextTemp.m_EndPoint);
        PointToView(canvas,EndPnt);

        int TextClr=GetColor(TextTemp.m_TextColor);
        int BackClr=GetColor(TextTemp.m_BackColor);

        Paint paint = new Paint();
        paint.setColor(TextClr);
        paint.setStrokeWidth(3);

        int Width=canvas.getWidth();
        int WidthEx= m_Jxt.m_Qjpmc_XmlCnfg.m_ScreenWidth;
        int FontSize = TextTemp.m_FontSize * Width *2 / WidthEx;
        paint.setTextSize(FontSize);

        if(TextTemp.m_AlignType==3) {
            paint.setTextAlign(Paint.Align.RIGHT);//右对齐
            canvas.drawText(TextTemp.m_Context, EndPnt.x, StartPnt.y, paint);
        }
        else if(TextTemp.m_AlignType==1) {
            paint.setTextAlign(Paint.Align.LEFT);//左对齐
            canvas.drawText(TextTemp.m_Context, StartPnt.x, StartPnt.y, paint);
        }
        else if(TextTemp.m_AlignType==2) {
            paint.setTextAlign(Paint.Align.CENTER);//中对齐
            Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();

            float baseline = (StartPnt.y+EndPnt.y - fontMetrics.bottom - fontMetrics.top) / 2;
            canvas.drawText(TextTemp.m_Context, (StartPnt.x + EndPnt.x) / 2, baseline, paint);
        }
    }

    protected void PointToView(Canvas canvas,PointF Pnt){

        int Width=canvas.getWidth();
        m_Width=canvas.getWidth();
        int WidthEx= m_Jxt.m_Qjpmc_XmlCnfg.m_ScreenWidth;

        Pnt.x=Pnt.x * Width  / WidthEx;
        Pnt.y=Pnt.y * Width  / WidthEx;
    }

    protected int GetColor(String strColor) {
        int Result = Color.argb(255,255,255,255);
        String[] strClr = strColor.split(",");
        if(strClr.length!=3) return Result;
        int Red = Integer.parseInt(strClr[0]);
        int Green = Integer.parseInt(strClr[1]);
        int Blue = Integer.parseInt(strClr[2]);
        Result = Color.argb(255,Red,Green,Blue);
        return Result;
    }

    protected PointF GetPoint(String strPoint) {
        PointF Pnt=new PointF(-1,-1);
        String[] strPos = strPoint.split(",");
        if(strPos.length!=2) return Pnt;
        Pnt.x = Float.parseFloat(strPos[0]);
        Pnt.y = Float.parseFloat(strPos[1]);
        return Pnt;
    }

    //绘图接口
    public void onDraw(Canvas canvas)
    {
        if(m_Jxt == null)
        {
            return;
        }
        if (m_Jxt.m_Qjpmc_XmlCnfg == null)
        {
            return;
        }
        if (m_Jxt.m_Qjpmc_XmlCnfg.m_Page == null)
        {
            return;
        }

        if (m_JxtPos >= m_Jxt.m_Qjpmc_XmlCnfg.m_Page.size())
        {
            return;
        }

        PwrDiagramObject.Page JxtPage = m_Jxt.m_Qjpmc_XmlCnfg.m_Page.get(m_JxtPos);
/*
            Active.tv_top_title.setText(JxtPage.m_PageName);
*/
        int BackClr=GetColor(JxtPage.m_BackColor);
        canvas.drawColor(BackClr);

        for(int i=0; i < JxtPage.m_RectNum && i< JxtPage.m_Rect.size(); i++)
        {
            PwrDiagramObject.Rect RectTemp=JxtPage.m_Rect.get(i);
            DrawRect(RectTemp, canvas);
        }
        for(int i = 0; i < JxtPage.m_CircleNum && i < JxtPage.m_Circle.size(); i++)
        {
            PwrDiagramObject.Circle CircleTemp=JxtPage.m_Circle.get(i);
            DrawCircle(CircleTemp, canvas);
        }
        for(int i =0; i < JxtPage.m_LineNum && i < JxtPage.m_Line.size(); i++)
        {
            PwrDiagramObject.Line LineTemp=JxtPage.m_Line.get(i);
            DrawLine(LineTemp, canvas);
        }
        for(int i = 0; i < JxtPage.m_ArcNum && i < JxtPage.m_Arc.size(); i++)
        {
            PwrDiagramObject.Arc ArcTemp=JxtPage.m_Arc.get(i);
            DrawArc(ArcTemp, canvas);
        }
        for(int i = 0; i < JxtPage.m_TextNum && i < JxtPage.m_Text.size(); i++)
        {
            PwrDiagramObject.Text TextTemp = JxtPage.m_Text.get(i);
            DrawText(TextTemp, canvas);
        }

        for(int i = 0; i < JxtPage.m_ElementNum && i < JxtPage.m_Element.size(); i++)
        {
            PwrDiagramObject.Element ElementTemp=JxtPage.m_Element.get(i);
            DrawElement(ElementTemp, canvas);
        }

        for(int i = 0; i < JxtPage.m_StateComptNum && i < JxtPage.m_StateComptList.size(); i++)
        {
            StateCompt StateComptTemp = JxtPage.m_StateComptList.get(i);
            DrawStateCompt(StateComptTemp, canvas, i);
        }

        for(int i = 0; i < JxtPage.m_ValueComptNum && i < JxtPage.m_ValueCompt.size(); i++)
        {
            PwrDiagramObject.ValueCompt ValueComptTemp=JxtPage.m_ValueCompt.get(i);
            DrawValueCompt(ValueComptTemp, canvas,i);
        }

        for(int i = 0; i < JxtPage.m_NormalBtnNum && i < JxtPage.m_NormalBtn.size(); i++) {
            PwrDiagramObject.NormalBtn BtnTemp=JxtPage.m_NormalBtn.get(i);
            DrawButton(BtnTemp, canvas);
        }

        //Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources()，R.drawable.roadmech);


        return;
    }

}



/*

public class CAnalogDataItem
{
    String m_name;
    int m_id = 0;

}

public class CDigitalDataItem
{
    String m_Name;
    int m_id;
}

//数据组
public class CDataTeam
{
    int m_Type;
    int m_DataNum;
}

//电力设备
public class CPwrDevice
{
    ;
}

public class CPwrStation{

    CPwrDevice m_HeadDevice;//头设备
}
*/
